<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Dialogs in PHP GTK</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, PHP, dialogs, GTK, programming, Linux">
<meta name="description" content="This part of the PHP GTK tutorial covers dialogs.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Dialogs</h1>


<p>
In this part of the PHP GTK programming tutorial, we will introduce dialogs. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications.
A dialog is defined as a conversation between two or more persons. In a computer
application a dialog is a window which is used to "talk" to the application.
A dialog is used to input data, modify data, change the application settings etc.
Dialogs are important means of communication between a user and a computer program.
</p>


<h2>GtkMessageDialog</h2>

<p>
Message dialogs are convenient dialogs that provide messages to 
the user of the application.
The message consists of textual and image data. The <code>GtkMessageDialog</code>
is used to create message dialogs. 
</p>

<pre class="code">
&lt;?php

/* 
ZetCode PHP GTK tutorial

This example demonstrates a
GtkMessageDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    public function __construct() { 

        parent::__construct(); 
         
        $this-&gt;init_ui();

    } 

    public function init_ui() {

        $this-&gt;set_title('GtkMessageDialog');         
        $this-&gt;connect_simple('destroy', array('gtk', 'main_quit')); 

        $fixed = new GtkFixed();              

        $button = new GtkButton("Information");
        $button-&gt;set_size_request($button-&gt;size_request());
        $button-&gt;connect('clicked', array($this, 'on_clicked'));
        
        $fixed-&gt;put($button, 50, 50);
        $this-&gt;add($fixed); 

        $this-&gt;set_default_size(250, 200); 
        $this-&gt;set_position(GTK::WIN_POS_CENTER);
        $this-&gt;show_all();         
    }

    public function on_clicked($sender) {

            $md = new GtkMessageDialog($this, Gtk::DIALOG_MODAL,
                Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, "Download completed.");
            $md-&gt;set_title("Information");    
            $md-&gt;run();
            $md-&gt;destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?&gt;
</pre>

<p>
We show a button on the window. When we click on the button, an information 
message is displayed. 
</p>

<pre class="explanation">
$button = new GtkButton("Information");
</pre>

<p>
This is a button, which will show a dialog, when we click on it.
</p>

<pre class="explanation">
public function on_clicked($sender) {

        $md = new GtkMessageDialog($this, Gtk::DIALOG_MODAL,
            Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, "Download completed.");
        $md-&gt;set_title("Information");    
        $md-&gt;run();
        $md-&gt;destroy();
}
</pre>

<p>
If we click on the info button, the Information dialog is displayed. 
The <code>Gtk::DIALOG_MODAL</code> flag makes the dialog modal. 
The <code>Gtk::MESSAGE_INFO</code> specifies the type of the dialog. 
In our case it is an information dialog. Different icons are chosen for
various dialog types. 
The <code>Gtk::BUTTONS_OK</code> shows the OK button on the dialog.
 The last parameter is the message displayed. 
We set the title of the dialog with the <code>set_title()</code> 
method. The dialog is displayed with the <code>run()</code> method. 
The programmer must also call either the <code>destroy()</code> or the 
<code>hide()</code> method in the end.
</p>


<img src="/img/gui/phpgtk/information.png" alt="Information message dialog">
<div class="figure">Figure: Information message dialog</div>



<h2>GtkAboutDialog</h2>

<p>
The <code>GtkAboutDialog</code> displays information about the application.
It can display a logo, the name of the application, version, copyright, 
website or license information. 
It is also possible to give credits to the authors, documenters, 
translators and artists.
</p>

<pre class="code">
&lt;?php

/* 
ZetCode PHP GTK tutorial

This example demonstrates the
AboutDialog dialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    public function __construct() { 

        parent::__construct(); 
         
        $this-&gt;init_ui();

    } 

    public function init_ui() {

        $this-&gt;set_title('About Battery');         
        $this-&gt;connect_simple('destroy', array('gtk', 'main_quit')); 

        $fixed = new GtkFixed();              

        $button = new GtkButton("About");
        $button-&gt;set_size_request(80, 30);
        $button-&gt;connect('clicked', array($this, 'on_clicked'));
        
        $fixed-&gt;put($button, 50, 50);
        $this-&gt;add($fixed); 

        $this-&gt;set_default_size(250, 200); 
        $this-&gt;set_position(GTK::WIN_POS_CENTER);
        $this-&gt;show_all();         
    }

    public function on_clicked($sender) {

        $about = new GtkAboutDialog();
        $about-&gt;set_program_name("Battery");
        $about-&gt;set_version("0.1");
        $about-&gt;set_copyright("(c) Jan Bodnar");
        $about-&gt;set_comments("Battery is a simple tool for battery checking");
        $about-&gt;set_website("http://www.zetcode.com");
        $about-&gt;set_logo(GdkPixbuf::new_from_file("battery.png"));
        $about-&gt;run();
        $about-&gt;destroy();
    }

} 
     
new Example(); 
Gtk::main();
 
?&gt;
</pre>

<p>
The code example uses a <code>GtkAboutDialog</code> with some of its features. 
</p>

<pre class="explanation">
$about = new GtkAboutDialog();
</pre>

<p>
We create an instance of the <code>GtkAboutDialog</code>. 
</p>

<pre class="explanation">
$about-&gt;set_program_name("Battery");
$about-&gt;set_version("0.1");
$about-&gt;set_copyright("(c) Jan Bodnar");
</pre>

<p>
Here we specify the name, the version and the copyright of the 
program.
</p>

<pre class="explanation">
$about-&gt;set_logo(GdkPixbuf::new_from_file("battery.png"));
</pre>

<p>
This line creates a logo. 
</p>

<img src="/img/gui/phpgtk/about.png" alt="GtkAboutDialog">
<div class="figure">Figure: GtkAboutDialog</div>



<h2>GtkFontSelectionDialog</h2>

<p>
The <code>GtkFontSelectionDialog</code> is a dialog for selecting fonts. 
It is typically used in applications, that do some 
text editing or formatting. 
</p>

<pre class="code">
&lt;?php

/* 
ZetCode PHP GTK tutorial

In this example, we change the font
of a label with the GtkFontSelectionDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
*/
 
class Example extends GtkWindow { 
     
    private $label;

    public function __construct() { 

        parent::__construct(); 
         
        $this-&gt;init_ui();

    } 

    private function init_ui() {

        $this-&gt;set_title('FontSelectionDialog');         
        $this-&gt;connect_simple('destroy', array('gtk', 'main_quit')); 

        $this-&gt;set_border_width(10);
        $this-&gt;label = new GtkLabel("The only victory over love is flight.");
        $button = new GtkButton("Select font");
        $button-&gt;connect('clicked', array($this, 'on_clicked'));

        $fixed = new GtkFixed();
        $fixed-&gt;put($button, 100, 30);
        $fixed-&gt;put($this-&gt;label, 30, 90);
        $this-&gt;add($fixed);
       
        $this-&gt;set_default_size(350, 200); 
        $this-&gt;set_position(GTK::WIN_POS_CENTER);
        $this-&gt;show_all();         
    }

    public function on_clicked($sender) {

        $fdia = new GtkFontSelectionDialog("Select font name");
        $response = $fdia-&gt;run();
              
        if ($response == Gtk::RESPONSE_OK) {

            $font_desc = new PangoFontDescription($fdia-&gt;get_font_name());
            print($fdia-&gt;get_font_name());
            if ($font_desc) {
                $this-&gt;label-&gt;modify_font($font_desc);
            }
            
        }

        $fdia-&gt;destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?&gt;
</pre>

<p>
In the code example, we have a button and a label. We show the 
<code>GtkFontSelectionDialog</code> by clicking on the button.
</p>

<pre class="explanation">
$fdia = new GtkFontSelectionDialog("Select font name");
</pre>

<p>
We create the <code>GtkFontSelectionDialog</code>.
</p>

<pre class="explanation">
if ($response == Gtk::RESPONSE_OK) {

    $font_desc = new PangoFontDescription($fdia-&gt;get_font_name());
    print($fdia-&gt;get_font_name());
    if ($font_desc) {
        $this-&gt;label-&gt;modify_font($font_desc);
    }
    
}
</pre>

<p>
If we click on the OK button, the font of the label widget changes to the
one, that we selected in the dialog. 
</p>

<img src="/img/gui/phpgtk/fontdialog.png" alt="GtkFontSelectionDialog">
<div class="figure">Figure: GtkFontSelectionDialog </div>


<h2>GtkColorSelectionDialog</h2>

<p>
The <code>GtkFontSelectionDialog</code> is a dialog for selecting colors.
</p>

<pre class="code">
&lt;?php

/* 
ZetCode PHP GTK tutorial

This example works with the
GtkColorSelectionDialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: August 2011
*/
 
class Example extends GtkWindow { 
     
    private $label;

    public function __construct() { 

        parent::__construct(); 
         
        $this-&gt;init_ui();

    } 

    private function init_ui() {

        $this-&gt;set_title('GtkColorSelectionDialog');         
        $this-&gt;connect_simple('destroy', array('gtk', 'main_quit')); 

        $this-&gt;set_border_width(10);
        $this-&gt;label = new GtkLabel("The only victory over love is flight.");
        $button = new GtkButton("Select color");
        $button-&gt;connect('clicked', array($this, 'on_clicked'));

        $fixed = new GtkFixed();
        $fixed-&gt;put($button, 100, 30);
        $fixed-&gt;put($this-&gt;label, 30, 90);
        $this-&gt;add($fixed);
       
        $this-&gt;set_default_size(350, 200); 
        $this-&gt;set_position(GTK::WIN_POS_CENTER);
        $this-&gt;show_all();         
    }

    public function on_clicked($sender) {

        $cdia = new GtkColorSelectionDialog("Select color");
        $response = $cdia-&gt;run();
              
        if ($response == Gtk::RESPONSE_OK) {
            $colorsel = $cdia-&gt;colorsel;
            $color = $colorsel-&gt;get_current_color();
            $this-&gt;label-&gt;modify_fg(Gtk::STATE_NORMAL, $color);
        }
        
        $cdia-&gt;destroy();
    }
} 
     
new Example(); 
Gtk::main();
 
?&gt;
</pre>

<p>
The example is very similar to the previous one. This time we change the 
color of the label.
</p>

<pre class="explanation">
$cdia = new GtkColorSelectionDialog("Select color");
$response = $cdia-&gt;run();
</pre>

<p>
We create and run the <code>GtkFontSelectionDialog</code>.
</p>

<pre class="explanation">
if ($response == Gtk::RESPONSE_OK) {
    $colorsel = $cdia-&gt;colorsel;
    $color = $colorsel-&gt;get_current_color();
    $this-&gt;label-&gt;modify_fg(Gtk::STATE_NORMAL, $color);
}
</pre>

<p>
If the user presses OK, we get the color value and modify the label's color.
</p>

<p>
In this part of the PHP GTK tutorial, we presented dialogs. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified September 8, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

